package com.alm.token.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alm.token.ConStant;
import com.alm.token.GlideApp;
import com.alm.token.MyApp;
import com.alm.token.R;
import com.alm.token.base.BaseActivity;
import com.alm.token.base.BaseAdapter;
import com.alm.token.common.RefreshLoadMoreManager;
import com.alm.token.ui.data.bean.KuaCurrencyListData;
import com.alm.token.ui.data.bean.MyIncomeData;
import com.alm.token.ui.data.bean.MyIncomeLogData;
import com.alm.token.ui.data.bean.UserWalletData;
import com.alm.token.ui.persenter.MyPreenter;
import com.alm.token.ui.view.KuaCurrencyView;
import com.alm.token.ui.view.ShouyiView;
import com.alm.token.utils.ActionBarHelper;
import com.alm.token.utils.CommonUtil;
import com.alm.token.utils.EmptyUtils;
import com.alm.token.utils.SpUtil;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class KuaCurrencyActivity extends BaseActivity implements KuaCurrencyView<KuaCurrencyListData> {
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.swipeRefreshLayout)
    SwipeRefreshLayout swipeRefreshLayout;
    @BindView(R.id.tv_clickreload)
    TextView tvClickreload;
    @BindView(R.id.nodatashow_body)
    RelativeLayout nodatashowBody;
    @BindView(R.id.centerItem)
    TextView centerItem;

    private RefreshLoadMoreManager<KuaCurrencyListData.DataBean.WalletsBean> manager;
    private List<KuaCurrencyListData.DataBean.WalletsBean> dataBeans = new ArrayList<>();

    private String limit = "100", page = "1", type = "2";

    @Override
    protected MyPreenter initPreenter() {
        return new MyPreenter(this);
    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_kuacurrencylist;
    }

    @Override
    protected void initVariables() {

    }

    @Override
    protected void initViews() {
        new ActionBarHelper(this).setTitle(getString(R.string.kua_hint_title));
        swipeRefreshLayout.setVisibility(View.GONE);
        initManager();
    }

    private void initManager() {
        GroupAdapter adapter = new GroupAdapter(this);
        manager = new RefreshLoadMoreManager.Builder(this)
                .setAdapter(adapter)
                .setRecyclerView(recyclerView)
                .setSwipeRefreshLayout(swipeRefreshLayout)
                .setOnRefreshListener(new RefreshLoadMoreManager.OnRefreshListener() {
                    @Override
                    public void onRefresh() {
//                        showToast("onRefresh");
                        swipeRefreshLayout.setRefreshing(false);
                        loadData();
                    }
                })
                .setOpenLoadMore(true)
                .setOnLoadMoreListener(new RefreshLoadMoreManager.OnLoadMoreListener() {
                    @Override
                    public void onLoadMore() {
                        manager.getAdapter().loadMoreEnd(true);
                        manager.getAdapter().loadMoreFail();
                        preenter.getKuaWalletsData(type);
                    }
                })
                .build();
//        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
//            @Override
//            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
//
//
//            }
//        });
        recyclerView.setNestedScrollingEnabled(false);
    }

    @Override
    protected void loadData() {
        page = "1";
        limit = "100";
        preenter.getKuaWalletsData(type);
//        manager.setData(getTestData());
    }


    @Override
    public void onClick(View v) {

    }

    @Override
    public void getData(KuaCurrencyListData kuaCurrencyListData) {
        if (kuaCurrencyListData.getStatus() == 0 && EmptyUtils.isNotEmpty(kuaCurrencyListData.getData())) {

            if (EmptyUtils.isNotEmpty(kuaCurrencyListData.getData().getWallets())) {
                dataBeans.clear();
                dataBeans.addAll(kuaCurrencyListData.getData().getWallets());

                for (KuaCurrencyListData.DataBean.WalletsBean walletsBean : dataBeans) {
                    walletsBean.setLayoutId(GroupAdapter.LAYOUT_ID);
                }

                manager.setData(dataBeans);
            }

        } else {
            showToast(kuaCurrencyListData.getMsg());
        }
        if (dataBeans.size() == 0) {
            swipeRefreshLayout.setVisibility(View.GONE);
            nodatashowBody.setVisibility(View.VISIBLE);
        } else {
            swipeRefreshLayout.setVisibility(View.VISIBLE);
            nodatashowBody.setVisibility(View.GONE);
        }
    }

    @Override
    public void jumpToLoginAct(int staue) {
        if (staue == 3) {
//            showToast(getString(R.string.common_relogin));
//            Intent intent = new Intent();
//            intent.setAction(ConStant.EXIT_APP);
//            sendBroadcast(intent);
//            startActivity(BlockWalletLoginActivity.class);
//            finish();
        } else if (staue == 4) {
//            showToast(getString(R.string.paypasw_hint_setpaypsw));
//            Intent intent = new Intent(getActivity(),PayPwdActivity.class);
//            intent.putExtra("PayPwdActivityType",PayPwdActivity.PAYPSW_SET);
//            startActivity(intent);
//            getActivity().finish();
            showToSetPaypswDialog();
        } else if (staue == 666) {
            showToUpdata();
        }
    }

    @Override
    public void onError(String error) {

        showToast(getString(R.string.common_loaderrritry));
    }

    @Override
    public void ShowProgress() {
        showWaitDialog();
    }

    @Override
    public void hideProgress() {
        hideWaitDialog();
    }

    @OnClick(R.id.tv_clickreload)
    public void onViewClicked() {
        loadData();
    }


    public class GroupAdapter extends BaseAdapter<KuaCurrencyListData.DataBean.WalletsBean> {

        public static final int LAYOUT_ID = R.layout.item_home_wallet;

        public GroupAdapter(@NonNull Context context) {
            super(context);
        }

        @Override
        protected void convert(BaseViewHolder helper, KuaCurrencyListData.DataBean.WalletsBean item, int itemType) {

            boolean canshown = SpUtil.getBoolean(MyApp.context, ConStant.NEEDRESHOWN, true);

            GlideApp.with(KuaCurrencyActivity.this)
                    .load(item.getLogo())
                    .listener(new RequestListener<Drawable>() {
                        @Override
                        public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                            return false;
                        }

                        @Override
                        public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
//                    if (EmptyUtils.isNotEmpty(resource)) {
//                        CommonUtil.changeSVGColor(getActivity(), helper.getView(R.id.img_bizhongicon), resource, R.color.black);
//                        return true;
//                    }
                            return false;
                        }
                    })
                    .error(R.drawable.icon_arctic)
                    .into((ImageView) helper.getView(R.id.img_bizhongicon));

//            GlideApp.with(getActivity()).asGif().load("https://p3.ssl.qhimgs1.com/sdr/400__/t016393d4f01fe0bf59.gif").error(R.drawable.icon_arctic)
//                    .into((ImageView) helper.getView(R.id.img_bizhongicon));

            helper.setText(R.id.name, item.getName());
            helper.setText(R.id.desc, item.getEngName());

            helper.setText(R.id.money1, canshown ? CommonUtil.saveToPoint4WithNo(item.getMoney()) + "" : "****");
            helper.setText(R.id.money2, CommonUtil.saveToPoint4WithNo(item.getPrice()) + "");
            helper.setOnClickListener(R.id.rly_currencyitem, new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    UserWalletData.DataBean.WalletsBean item1 =new UserWalletData.DataBean.WalletsBean();
                    item1.setName(item.getName());
                    item1.setLogo(item.getLogo());
                    item1.setId(item.getId());
                    Intent intent = new Intent(KuaCurrencyActivity.this, CurrencyDetailActivity.class);
                    intent.putExtra("WalletsBean",item1 );
                    intent.putExtra("intentType",CurrencyDetailActivity.INTENTTYPE_KUA );
                    startActivity(intent);

                }
            });
        }
    }
}
